home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / GdmGreeter / services.py < prev    next >
Encoding:
Python Source  |  2013-01-10  |  5.3 KB  |  145 lines

  1. #!/usr/bin/python
  2. #
  3. # Copyright 2011 Max <govnototalitarizm@gmail.com>
  4. # Copyright 2011 Martin Owens
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. #  it under the terms of the GNU General Public License as published by
  8. #  the Free Software Foundation, either version 3 of the License, or
  9. #  (at your option) any later version.
  10. #
  11. #  This program is distributed in the hope that it will be useful,
  12. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #  GNU General Public License for more details.
  15. #
  16. #  You should have received a copy of the GNU General Public License
  17. #  along with this program.  If not, see <http://www.gnu.org/licenses/>
  18. #
  19. """Provides access to gdm services for python greeters
  20.  
  21. """
  22.  
  23. import os, time, logging, locale
  24. import dbus, dbus.bus, dbus.exceptions
  25. from dbus.mainloop.glib import DBusGMainLoop
  26.  
  27. DBusGMainLoop(set_as_default = True)
  28.  
  29. PASSWD = '/etc/passwd'
  30.  
  31. class GdmDbusService(object):
  32.     """Basic wrapper to create the correct interfaces and proxy signals"""
  33.     def __init__(self):
  34.         try:
  35.             # The first is access to the object and any interfaces.
  36.             self.raw = self.connection.get_object(self.address, self.path)
  37.         except dbus.exceptions.DBusException:
  38.             logging.error("Failed to get Dbus object %s", self.path)
  39.             raise
  40.         # The second is access to the interface, which is more interesting.
  41.         self.obj = dbus.Interface(self.raw, self.interface)
  42.         # Watch all signals on this interface
  43.         self.connection.add_signal_receiver(self.got_signal,
  44.             dbus_interface = self.interface, signal_name = None, bus_name = None, path = self.path,
  45.             sender_keyword = "sent", destination_keyword = "dest", interface_keyword = None,
  46.             member_keyword = "name", path_keyword = None, message_keyword = None
  47.         )
  48.  
  49.     def got_signal(self, *args, **kwargs):
  50.         """Log all signals and call the proxy method for it."""
  51.         name = kwargs.pop('name', 'Unknown')
  52.         # Call the object's signal proxy method
  53.         if hasattr(self, name):
  54.             getattr(self, name)( *args )
  55.             log = logging.debug
  56.         else:
  57.             log = logging.warn
  58.         # For debugging and for warnings.
  59.         for key in kwargs.keys():
  60.             if kwargs[key] == None:
  61.                 kwargs.pop(key)
  62.         if kwargs:
  63.             log( "%s:%s%s %s" % (self.interface, name, str(args), str(kwargs)) )
  64.         else:
  65.             log("%s:%s%s" % (self.interface, name, str(args)))
  66.  
  67.  
  68. class GdmGreeterService(GdmDbusService):
  69.     """general greeter class"""
  70.     path = '/org/gnome/DisplayManager/GreeterServer'
  71.     address = 'org.gnome.DisplayManager.GreeterServer'
  72.     interface = 'org.gnome.DisplayManager.GreeterServer'
  73.  
  74.     def __init__(self, *args, **kwargs):
  75.         address = kwargs.pop('address', os.environ['GDM_GREETER_DBUS_ADDRESS'])
  76.         try:
  77.             self.connection = dbus.connection.Connection(address)
  78.             logging.debug("Connected to Greeter-Service on %s", address)
  79.         except dbus.exceptions.DBusException:
  80.             logging.error("Failed to connect to Greeter-Service on %s", address)
  81.             raise
  82.         GdmDbusService.__init__(self)
  83.         self.display = GdmDisplay(self.obj.GetDisplayId())
  84.  
  85.     def SelectLanguage(self, lang):
  86.         """Call into GdmGreeter to change the language"""
  87.         if '.' not in lang:
  88.             lang += '.UTF8'
  89.         lang = locale.normalize(lang).replace('UTF8', 'UTF-8')
  90.         logging.debug("Setting language to %s", lang)
  91.         self.obj.SelectLanguage(locale.normalize(lang))
  92.  
  93.     def SelectLayout(self, layout):
  94.         """Call into GdmGreeter to change the layout"""
  95.         logging.debug("Setting session layout to %s", layout)
  96.         if layout: self.obj.SelectLayout(layout)
  97.         else: logging.debug("Ignored %s", layout)
  98.  
  99.     def Ready(self):
  100.         """Called when greeter service is ready"""
  101.         logging.debug("GdmServer is Ready")
  102.         self.obj.BeginVerification()
  103.  
  104.     def InfoQuery(self, text):
  105.         """Server wants to ask the user for something (username normally)"""
  106.         raise NotImplementedError("You need to handle InfoQuery to ask for the username.")
  107.  
  108.     def SecretInfoQuery(self, text):
  109.         """Server wants to ask for some secrate info (password normally)"""
  110.         raise NotImplementedError("You need to handle SecretInfoQuery to ask for the password.")
  111.  
  112.     def UserAuthorized(self):
  113.         """User is ready to go, lets get them in"""
  114.         self.obj.StartSessionWhenReady( True )
  115.         self.FinishProcess()
  116.  
  117.     def BeginAutologin(self, username):
  118.         """Attempt autologin"""
  119.         self.obj.BeginAutologin(username)
  120.  
  121.     def FinishProcess(self):
  122.         """Called when we're logging into the client"""
  123.         pass
  124.  
  125.  
  126. class GdmDisplay(GdmDbusService):
  127.     """Connect to the display bus"""
  128.     address = 'org.gnome.DisplayManager'
  129.     interface = 'org.gnome.DisplayManager.Display'
  130.  
  131.     def __init__(self, path):
  132.         self.connection = dbus.SystemBus()
  133.         self.path = path
  134.         super(GdmDisplay, self).__init__()
  135.  
  136.     @property
  137.     def name(self):
  138.         """obtain X11 display name"""
  139.         return self.obj.GetX11DisplayName()
  140.  
  141.     @property
  142.     def number(self):
  143.         """obtain X11 display number"""
  144.         return self.obj.GetX11DisplayNumber()
  145.